package com.jeeplus.modules.monitor.task;

import com.jeeplus.modules.monitor.entity.ScheduleJob;
import com.jeeplus.modules.monitor.entity.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @author Administrator
 * @title: TaskShell
 * @projectName jeeplus
 * @description: TODO
 * @date 2019/8/716:13
 */

public class TaskShell extends Task {
    /*日志*/
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void run(ScheduleJob scheduleJob) {
        Process process = null;
        try {
            logger.info(String.format("【%s】",scheduleJob.getName()));
            process = Runtime.getRuntime().exec(scheduleJob.getParameter());
        } catch (IOException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        InputStream is = process.getInputStream();
        String line;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(is, "GBK"));
            while ((line = br.readLine()) != null) {
                logger.info(line);
            }
            br.close();
            process.getOutputStream().close();
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }


    }
}
